<?php

// 本类由系统自动生成，仅供测试用途

namespace Home\Controller;

use Think\Controller;

class BaseController extends Controller {

    public function __construct() {
        parent::__construct();
        if (!$_SESSION["member"]) {
            redirect("/Login");
        } else {
            $this->member = $_SESSION["member"];
            $this->assign("member", $this->member);
            $this->app = $_SESSION["app"];
            $this->assign("app", $this->app);
            $this->apps = $_SESSION["apps"];
            $this->assign("apps", $this->apps);
        }
    }

    //获取用户列表
    public function user_list($next = '') {
        return myCurl(C('API_URL') . '/user/get?access_token=' . token() . '&next_openid=' . $next);
    }

    //获取用户基本信息
    public function user_base($openid) {
        return myCurl(C('API_URL') . '/user/info?access_token=' . token() . '&openid=' . $openid . '&lang=zh_CN');
    }

    //获取的同时并保存到数据库  可以批量
    protected function get_user($openid) {
        $mod = D('User');
        if (is_array($openid)) {
            $data = $mod->where(array('openid' => array('in', $openid)))->select();
            foreach ($data as &$v) {
                $return[$v['openid']] = $v;
                $hava[] = $v['openid'];
            }
            if (!empty($hava))
                $openid = array_diff($openid, $hava);
            foreach ($openid as &$vv) {
                $return[$vv] = $this->user_base($vv);
                $return[$vv]['app_id'] = $this->app['id'];
                $adds[] = $mod->create($return[$vv]);
            }
        } else {
            $data = $mod->where(array('openid' => $openid))->find();
            if (empty($data)) {
                $return[$openid] = $this->user_base($openid);
                $return[$openid]['app_id'] = $this->app['id'];
                $adds[] = $mod->create($return[$openid]);
            }
        }
        if (!empty($adds))
            $mod->addAll($adds);
        return $return;
    }

    //获取组列表
    public function group_list() {
        return myCurl(C('API_URL') . '/groups/get?access_token=' . token());
    }

    //获取用户所在组
    public function user_to_group($data) {
        return myCurl(C('API_URL') . '/groups/getid?access_token=' . token(), json_encode($data));
    }

    //添加分组
    public function add_wx_group($data) {
        return myCurl(C('API_URL') . '/groups/create?access_token=' . token(), json_encode(array('group' => $data)));
    }

    //修改分组
    public function edit_wx_group($data) {
        return myCurl(C('API_URL') . '/groups/update?access_token=' . token(), json_encode(array('group' => $data)));
    }

    //移动组内用户
    public function user_to_new_group($data) {
        return myCurl(C('API_URL') . '/groups/members/update?access_token=' . token(), json_encode($data));
    }

    //更新菜单
    public function edit_wx_menu($data) {
        $json_utf8 = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($data));

        return myCurl(C('API_URL') . '/menu/create?access_token=' . token(), $json_utf8);
    }

    //查询菜单
    public function get_wx_menu() {
        return myCurl(C('API_URL') . '/menu/get?access_token=' . token());
    }

    //删除菜单
    public function del_menu() {
        return myCurl(C('API_URL') . '/menu/delete?access_token=' . token());
    }

    //获取二维码ticket
    public function get_ticket($data) {
        return myCurl(C('API_URL') . '/qrcode/create?access_token=' . token(), json_encode($data));
    }

    //上传多媒体
    public function upload_thing($picUrl, $type) {
        $filedata = array("media" => "@" . '/usr/local/apache2/htdocs/test/wx' . $picUrl);
        $url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . token() . '&type=' . $type;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $info = curl_exec($ch);

        if ($error = curl_error($ch)) {
            die($error);
        }
        curl_close($ch);
        return $info;
    }

}